package borge.web.teamspirit.client.page.shared.widgets;

import java.util.List;

import borge.web.teamspirit.client.TeamSpirit;
import borge.web.teamspirit.client.TeamSpiritConstants;
import borge.web.teamspirit.client.data.Person;
import borge.web.teamspirit.client.interfaces.PersistentData;
import borge.web.teamspirit.client.interfaces.ServerCallbackHandler;
import borge.web.teamspirit.client.interfaces.PersistentData.DataKey;
import borge.web.teamspirit.client.servercomm.DBQuery;
import borge.web.teamspirit.client.servercomm.DBQueryResults;
import borge.web.teamspirit.client.servercomm.ServerComm;
import borge.web.teamspirit.client.servercomm.DBQueryResults.Result;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ShowPersonWidget extends ShowDataWidget {

	private Person iPerson;
	private String iNewPersKey;
	
	private VerticalPanel iPanel;
	private FlexTable iTable;
	
	public ShowPersonWidget(){
		super(null);
		
		iPanel = new VerticalPanel();
		iTable = new FlexTable();
		iPanel.add(iTable);
		initWidget(iPanel);
		
		// personal info
		// which teams we own
		// which associations we own
		// which teams we are registered as player in
	}
	
	private void updateData(){
		
		int row = 0, column = 0;
		
		TeamSpiritConstants tsc = TeamSpirit.getConstants();
		iTable.setText(row, column++, tsc.first_name());
		iTable.setText(row++, column--, iPerson.getFirstName());
		
		iTable.setText(row, column++, tsc.last_name());
		iTable.setText(row++, column--, iPerson.getLastName());
		
		iTable.setText(row, column++, tsc.nick_name());
		iTable.setText(row++, column--, iPerson.getNickName());
		
		iTable.setText(row, column++, tsc.phone());
		iTable.setText(row++, column--, iPerson.getPhone());
		
		iTable.setText(row, column++, tsc.email());
		iTable.setText(row++, column--, iPerson.getEmail());
		
		iTable.setText(row++, column, "Owned teams");
		
		List<String> iOwnedTeams = iPerson.getOwnedTeams();
		for(String team : iOwnedTeams){
			iTable.setText(row++, column, team);
		}
		
		iTable.setText(row++, column, "Plays in teams");
		
		List<String> iTeams = iPerson.getTeamNames();
		for(String team : iTeams){
			iTable.setText(row++, column, team);
		}
		
		iTable.setText(row++, column, "Owned associations");
		
		List<String> iOwnedAss = iPerson.getAssociationNames();
		for(String ass : iOwnedAss){
			iTable.setText(row++, column, ass);
		}
		
	}
	
	@Override
	public boolean needsUpdate(String aData) {

		if(iPerson == null || !iPerson.getEncodedKey().equals(aData)){
			iNewPersKey = aData;
			return true;
		}
		
		return false;
	}

	@Override
	public void populateData(DBQueryResults results) {
		
		Result res = results.getResult(DataKey.PERSON);
		if(res.queryFailed()){
			Window.alert("Failed: " + res.getErrorMsg());
		}
		else{
			List<PersistentData> list = res.getResultList();
			iPerson = (Person)list.get(0);
			updateData();
		}

	}

	@Override
	public void requestServerData(ServerCallbackHandler aHandler) {
		// TODO Auto-generated method stub
		DBQuery query = new DBQuery(DataKey.PERSON, Person.class.getName());
		query.addFilter("iEncodedKey", DBQuery.EQUALS, "personKeyParam","String", iNewPersKey, null);
		ServerComm.getObjectList(query, aHandler);
	}

}
